package com.jzr.open.db1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzr.open.common.SecurityHelper;
import com.jzr.open.db1.mapper.TBedMapper;
import com.jzr.open.db1.mapper.TPatientMapper;
import com.jzr.open.db1.model.TBed;
import com.jzr.open.db1.model.TPatient;
import com.jzr.open.db1.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<TPatientMapper,TPatient> implements PatientService {
    @Autowired
    private TBedMapper bedMapper;

    public List<Integer> getIdList(Wrapper wrapper) {
        return baseMapper.getIdList(wrapper);
    }

    public Integer getId(Wrapper wrapper) {
        List<Integer> list = getIdList(wrapper);
        return list == null || list.size() <= 0 ? null : list.get(0);
    }
    
    @Override
    public int save(List<TPatient> list) {
        if(list == null || list.isEmpty()){
            return 0;
        }
        Integer userId = SecurityHelper.getUserId();
        int count = 0;
        for(TPatient record : list){
            QueryWrapper<TPatient> qw = new QueryWrapper<>();
            qw.eq(TPatient.Code_,record.getCode())
                    .eq(TPatient.CureNo_,record.getCureNo());

            if(record.getIsDel() != null && record.getIsDel().booleanValue() == true){
                count += getId(qw);
                continue;
            }
            record.setUpdateBy(userId);
            record.setUpdateTime(LocalDateTime.now());
            Integer id = getId(qw);
            if(id == null){
                count += baseMapper.insert(record);
            }else{
                record.setId(id);
                count += baseMapper.updateById(record);
            }

            //检查床位
            TBed bed = new TBed();
            bed.setDeptCode(record.getDeptCode());
            bed.setRoomCode(record.getRoomCode());
            bed.setBedCode(record.getBedCode());

            List<Integer> idList = bedMapper.getIdList(new QueryWrapper<TBed>()
                    .eq(TBed.DeptCode_,record.getDeptCode())
                    .eq(TBed.RoomCode_,record.getRoomCode())
                    .eq(TBed.BedCode_,record.getBedCode())
            );
            if(idList == null || idList.size()<=0){
                bed.setPatientId(record.getId());
                bedMapper.insert(bed);
            }else{
                bed.setId(idList.get(0));
                bed.setPatientId(record.getId());
                bedMapper.updateById(bed);
            }
        }
        return count;
    }

    @Override
    public int delete(List<TPatient> list) {
        if(list == null || list.isEmpty()){
            return 0;
        }
        int count = 0;
        for(TPatient record : list){
            QueryWrapper<TPatient> qw = new QueryWrapper<>();
            qw.eq(TPatient.Code_,record.getCode())
                    .eq(TPatient.CureNo_,record.getCureNo());
            count += baseMapper.delete(qw);
        }
        return count;
    }
}
